<html>
<head><meta charset="utf-8"><title>Disabling tail call optimisation? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html">Disabling tail call optimisation?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205698716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205698716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205698716">(Aug 01 2020 at 21:19)</a>:</h4>
<p>I've been looking into why the <code>__rust_begin_short_backtrace</code> frame was missing from some test runs, and have found that in the distributed <code>libtest.rlib</code> there appears to have been some tail-call optimisation where the preceding function jumps directly into <code>__rust_begin_short_backtrace</code> rather than calling it.  I didn't think rustc performed any TCO yet?  Is there any way to disable this behaviour?</p>



<a name="205698764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205698764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205698764">(Aug 01 2020 at 21:20)</a>:</h4>
<p>LLVM and rustc are free to perform TCO, it's just not guaranteed to happen</p>



<a name="205698769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205698769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205698769">(Aug 01 2020 at 21:20)</a>:</h4>
<p>Can it be disabled?</p>



<a name="205698834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205698834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205698834">(Aug 01 2020 at 21:23)</a>:</h4>
<p>maybe by passing an LLVM argument</p>



<a name="205698957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205698957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205698957">(Aug 01 2020 at 21:27)</a>:</h4>
<p>Okay thanks, I'll look into that.  I had gathered from <a href="https://seanchen1991.github.io/posts/tco-story/">https://seanchen1991.github.io/posts/tco-story/</a> that none should be performed at all, but perhaps that article is outdated or incorrect.</p>



<a name="205699005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205699005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205699005">(Aug 01 2020 at 21:29)</a>:</h4>
<p>However, given that the <code>__rust_begin_short_backtrace</code> frame needs to be present for <code>RUST_BACKTRACE=1</code> to terminate correctly, perhaps this optimisation is a bug?  Should I file an issue?</p>



<a name="205699065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205699065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205699065">(Aug 01 2020 at 21:30)</a>:</h4>
<p>I don't think the <code>short_backtrace</code> has ever worked correctly, really. Maybe for a short while after the initial implementation.</p>



<a name="205699077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205699077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205699077">(Aug 01 2020 at 21:31)</a>:</h4>
<p>Shouldn't unwind info help with reconstructing what's going on even with tail calls?</p>



<a name="205699133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205699133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205699133">(Aug 01 2020 at 21:33)</a>:</h4>
<p>No unwind info can't reconstruct this. What if two functions tail called to the same function? There is no way to know which function was tail called to the current funtion.</p>



<a name="205699189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205699189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205699189">(Aug 01 2020 at 21:34)</a>:</h4>
<p>You should be able to prevent the tail call by adding something like <code>read_volatile(&amp;0)</code> or <code>black_box(0)</code> after the call.</p>



<a name="205700031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205700031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205700031">(Aug 01 2020 at 22:02)</a>:</h4>
<p>Great, that looks like it's done it—thanks <span class="user-mention" data-user-id="133247">@bjorn3</span>.  If I submit a PR to fix <code>libtest</code>, who would be a suitable reviewer?</p>



<a name="205714574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Disabling%20tail%20call%20optimisation%3F/near/205714574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Disabling.20tail.20call.20optimisation.3F.html#205714574">(Aug 02 2020 at 06:29)</a>:</h4>
<p>you can let the highfive bot find a reviewer for you by not specifying one</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>